home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / gg / ncurses-5.3.lha / ncurses-5.3 / include / MKkey_defs.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  2002-10-24  |  5KB  |  179 lines

  1. #! /bin/sh
  2. # $Id: MKkey_defs.sh,v 1.10 2002/09/28 23:32:16 tom Exp $
  3. ##############################################################################
  4. # Copyright (c) 2001,2002 Free Software Foundation, Inc.                     #
  5. #                                                                            #
  6. # Permission is hereby granted, free of charge, to any person obtaining a    #
  7. # copy of this software and associated documentation files (the "Software"), #
  8. # to deal in the Software without restriction, including without limitation  #
  9. # the rights to use, copy, modify, merge, publish, distribute, distribute    #
  10. # with modifications, sublicense, and/or sell copies of the Software, and to #
  11. # permit persons to whom the Software is furnished to do so, subject to the  #
  12. # following conditions:                                                      #
  13. #                                                                            #
  14. # The above copyright notice and this permission notice shall be included in #
  15. # all copies or substantial portions of the Software.                        #
  16. #                                                                            #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
  20. # THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
  22. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
  23. # DEALINGS IN THE SOFTWARE.                                                  #
  24. #                                                                            #
  25. # Except as contained in this notice, the name(s) of the above copyright     #
  26. # holders shall not be used in advertising or otherwise to promote the sale, #
  27. # use or other dealings in this Software without prior written               #
  28. # authorization.                                                             #
  29. ##############################################################################
  30. #
  31. # MKkey_defs.sh -- generate function-key definitions for curses.h
  32. #
  33. # Author: Thomas E. Dickey <dickey@herndon4.his.com> 2001
  34. #
  35. # Extract function-key definitions from the Caps file
  36. #
  37. : ${AWK-awk}
  38. DATA=${1-Caps}
  39.  
  40. data=data$$
  41. pass1=pass1_$$
  42. pass2=pass2_$$
  43. pass3=pass3_$$
  44. pass4=pass4_$$
  45. trap 'rm -f $data pass[1234]_$$' 0 1 2 5 15
  46.  
  47. # change repeated tabs (used for readability) to single tabs (needed to make
  48. # awk see the right field alignment of the corresponding columns):
  49. if sort -k 6 $DATA >$data 2>/dev/null
  50. then
  51.     # POSIX
  52.     sed -e 's/[    ]\+/    /g' < $DATA |sort -n -k 6 >$data
  53. else
  54.     # SunOS (and SVr4, marked as obsolete but still recognized)
  55.     sed -e 's/[    ]\+/    /g' < $DATA |sort -n +5 >$data
  56. fi
  57.  
  58. # add keys that we generate automatically:
  59. cat >>$data <<EOF
  60. key_resize    kr1    str    R1    KEY_RESIZE    +    -----    Terminal resize event
  61. key_event    kv1    str    V1    KEY_EVENT    +    -----    We were interrupted by an event
  62. EOF
  63.  
  64. cat <<EOF
  65. /*
  66.  * These definitions were generated by $0 $DATA
  67.  */
  68. EOF
  69.  
  70. # KEY_RESET
  71. maxkey=345
  72.  
  73. for pass in 1 2 3 4
  74. do
  75.  
  76. output=pass${pass}_$$
  77.  
  78. ${AWK-awk} >$output <$data '
  79. function print_cols(text,cols) {
  80.     printf "%s", text
  81.     len = length(text);
  82.     while (len < cols) {
  83.         printf "    "
  84.         len += 8;
  85.     }
  86. }
  87. function decode(keycode) {
  88.     result = 0;
  89.     if (substr(keycode, 1, 2) == "0x") {
  90.         digits="0123456789abcdef";
  91.     } else if (substr(keycode, 1, 1) == "0") {
  92.         digits="01234567";
  93.     } else {
  94.         digits="0123456789";
  95.     }
  96.     while (length(keycode) != 0) {
  97.         digit=substr(keycode, 1, 1);
  98.         keycode=substr(keycode, 2);
  99.         result = result * length(digits) + index(digits, digit) - 1;
  100.     }
  101.     return result;
  102. }
  103.  
  104. BEGIN    {
  105.     maxkey='$maxkey';
  106.     pass='$pass';
  107.     key_max=1;
  108.     bits=1;
  109.     while (key_max < maxkey) {
  110.         bits = bits + 1;
  111.         key_max = (key_max * 2) + 1;
  112.     }
  113.     octal_fmt = sprintf ("%%0%do", (bits + 2) / 3 + 1);
  114. }
  115.  
  116. /^$/        {next;}
  117. /^#/        {next;}
  118. /^capalias/    {next;}
  119. /^infoalias/    {next;}
  120.  
  121. $5 != "-" && $6 != "-" {
  122.         if ($6 == "+") {
  123.             if (pass == 1 || pass == 2)
  124.                 next;
  125.             thiskey=maxkey + 1;
  126.         } else {
  127.             if (pass == 3)
  128.                 next;
  129.             thiskey=decode($6);
  130.         }
  131.         if (thiskey > maxkey)
  132.             maxkey = thiskey;
  133.         if (pass == 2 || pass == 3) {
  134.             showkey=sprintf(octal_fmt, thiskey);
  135.             if ($5 == "KEY_F(0)" ) {
  136.                 printf "#define "
  137.                 print_cols("KEY_F0", 16);
  138.                 print_cols(showkey, 16);
  139.                 print "/* Function keys.  Space for 64 */";
  140.                 printf "#define "
  141.                 print_cols("KEY_F(n)", 16);
  142.                 print_cols("(KEY_F0+(n))", 16);
  143.                 print "/* Value of function key n */"
  144.             } else {
  145.                 printf "#define "
  146.                 print_cols($5, 16);
  147.                 print_cols(showkey, 16);
  148.                 printf "/*"
  149.                 for (i = 8; i <= NF; i++)
  150.                     printf " %s", $i
  151.                 print " */"
  152.             }
  153.         }
  154.     }
  155. END    {
  156.         if (pass == 1) {
  157.             print maxkey;
  158.         } else if (pass == 4) {
  159.             print "";
  160.             printf "#define ";
  161.             print_cols("KEY_MAX", 16);
  162.             result = sprintf (octal_fmt, key_max);
  163.             print_cols(result, 16);
  164.             printf "/* Maximum key value is ";
  165.             printf octal_fmt, maxkey;
  166.             print " */";
  167.         }
  168.     }
  169. '
  170. if test $pass = 1 ; then
  171.     maxkey=`cat $pass1`
  172. fi
  173.  
  174. done
  175.  
  176. cat $pass2
  177. cat $pass3
  178. cat $pass4
  179.